package com.sxy.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

//GamePanel继承JPanel，才成为一个面板
public class GamePanel extends JPanel {
    //定义两个数组  一个存储蛇的X轴坐标，一个存储Y轴坐标
    int length;
    int[] snakeX = new int[200];
    int[] snakeY = new int[200];
    boolean isStart = false;//默认游戏暂停
    Timer  timer;

    String direction; //定义蛇的行走方向
//定义食物坐标
    int foodX;
    int foodY;
    int score;//定义积分
    //加入变量判断死亡
    boolean isDie=false;


    public void init() {
        //初始化蛇头和第一节身子坐标和第二节坐标
        //初始化蛇的长度
        length = 3;
        snakeX[0] = 175;
        snakeY[0] = 275;
        snakeX[1] = 150;
        snakeY[1] = 275;
        snakeX[2] = 125;
        snakeY[2] = 275;
        //初始化蛇头方向
        direction = "R";// U D L R
        foodX=300;
        foodY=200;

    }

    public GamePanel() {
        init();
        this.setFocusable(true);
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {              //监听键盘按下操作
                super.keyPressed(e);
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_SPACE) {                //监听空格
                    if (isDie) {//重绘动作
                           init();
                           isDie=false;
                    } else {
                        isStart = !isStart;
                        repaint();
                    }
                }
                if(keyCode==KeyEvent.VK_UP){
                    direction="U";
                }//up
                if(keyCode==KeyEvent.VK_DOWN){
                    direction="D";
                } //down
                if(keyCode==KeyEvent.VK_LEFT){
                    direction="L";
                }//left
                if(keyCode==KeyEvent.VK_RIGHT){
                    direction="R";
                } //right
            }
        });
//定时器初始化
        timer =new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isStart&&isDie==false){
                    for(int i=length-1;i>0;i--){
                        snakeX[i]=snakeX[i-1];
                        snakeY[i]=snakeY[i-1];
                    }
                    //动头：
                    if("R".equals(direction)){
                        snakeX[0]+=25;
                    }
                    if("L".equals(direction)){
                        snakeX[0]-=25;
                    }
                    if("U".equals(direction)){
                        snakeY[0]-=25;
                    }
                    if("D".equals(direction)){
                        snakeY[0]+=25;
                    }
                    //防止超出边界
                    if(snakeX[0]>750){
                        snakeX[0]=25;
                    }
                    if(snakeX[0]<25){
                        snakeX[0]=750;
                    }
                    if (snakeY[0]<100){
                        snakeY[0]=725;
                    }
                    if (snakeY[0]>725){
                        snakeY[0]=100;
                    }
                    //食物坐标和蛇头坐标重合
                    if (snakeX[0] == foodX && snakeY[0]==foodY) {
                        length++;
                        foodX= ((int)(Math.random()*30)+1)*25;
                        foodY= (new Random().nextInt(26)+4)*25;
                        score+=10;
                    }
                    //死亡判定
                    for(int i =1;i<length;i++){
                        if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                            isDie=true; //死亡状态
                        }
                    }
                    repaint();//重绘
                }
            }
        });
//
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        //paintComponent自动调用//
        super.paintComponent(g);
        //填充背景颜色//
        this.setBackground(new Color(208, 225, 231));
        //画头部图片
        Images.headerImg.paintIcon(this, g, 150, 8);
        g.setColor(new Color(219, 226, 219));
        g.fillRect(10, 110, 778, 640);
        //画蛇头
        if ("R".equals(direction)) {
            Images.rightImg.paintIcon(this, g, snakeX[0], snakeY[0]);
        }
        if ("L".equals(direction)) {
            Images.leftImg.paintIcon(this, g, snakeX[0], snakeY[0]);
        }
        if ("U".equals(direction)) {
            Images.upImg.paintIcon(this, g, snakeX[0], snakeY[0]);
        }
        if ("D".equals(direction)) {
            Images.downImg.paintIcon(this, g, snakeX[0], snakeY[0]);
        }
        //画第一节身子
        // Images.bodyImg.paintIcon(this,g,snakeX[1],snakeY[1]);
        //画第二节身子
        // Images.bodyImg.paintIcon(this,g,snakeX[2],snakeY[2]);//
        for (int i = 1; i < length; i++) {
            Images.bodyImg.paintIcon(this, g, snakeX[i], snakeY[i]);
        }
        //若游戏界面暂停，界面有句提示语
        if (isStart == false) {
            g.setColor(new Color(111, 81, 182));
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//字体，加粗，字号
            g.drawString("点击空格   开始游戏",250,330);
        }
        //画食物
        Images.footImg.paintIcon(this,g,foodX,foodY);
        //画积分
        g.setColor(new Color(255, 248, 182));
        g.setFont(new Font("微软雅黑",Font.BOLD,30));//字体，加粗，字号
        g.drawString("积分："+score,620,35);

        //画死亡状态
        if(isDie){
            g.setColor(new Color(193, 63, 88));
            g.setFont(new Font("微软雅黑",Font.BOLD,20));//字体，加粗，字号
            g.drawString("死亡，游戏终止，按下空格重新游戏",200,330);
        }
    }
}